#!/binbash

<<comment
环境自行准备如下
node1	192.168.88.11/24
node2	192.168.88.12/24
node3	192.168.88.13/24
client1	192.168.88.10/24
注意！！！node1-node3各额外再添加2块20GB的硬盘
各节点务必关闭selinux和防火墙  自行配置系统yum源，cephyum源
需自行配置各节点免密登陆；如需配置免密登陆，可开启35-39行；
此脚本可任意节点执行
comment

node1=192.168.88.11
node2=192.168.88.12
node3=192.168.88.13
client1=192.168.88.10
# 将node1作为部署节点  配置免密登陆
for i in $(tail -4 /etc/hosts | tr -s " " | cut -d" " -f2)
do
case $i in
client1|node1|node2|node3)
continue;;
*)
cat >> /etc/hosts <<EOF
192.168.88.10    client1
192.168.88.11    node1
192.168.88.12    node2
192.168.88.13    node3
EOF
break
esac
done

#[ -f /root/.ssh/id_rsa.pub ] || ssh-keygen -t rsa -b 2048 -N '' -f /root/.ssh/id_rsa && chmod 0400 /root/.ssh/id_rsa
#for i in {10..13}
#do
#ssh-copy-id 192.168.88.$i 2> /dev/null
#done

# 在节点上安装软件包
for hn in node{1..3}
do
scp /etc/hosts $hn:/etc/ &> /dev/null
ssh $hn "yum install -y ceph-mon ceph-osd ceph-mds ceph-radosgw" &> /dev/null
done

# 配置ntp服务器 确保时钟同步

echo -e "\033[36m正在设置时钟同步\033[0m"
ssh $client1 "yum -y install chrony;sed -i '26a allow 192.168.88.0/24' /etc/chrony.conf;sed -i '30s/#//' /etc/chrony.conf;systemctl restart chronyd" &> /dev/null 
for hn in node{1..3}
do
ssh $hn "yum -y install chrony;sed -i '/^server/s/s/# s/' /etc/chrony.conf;sed -i '6a server 192.168.88.10 iburst' /etc/chrony.conf;systemctl restart chronyd" &> /dev/null
done
sleep 3
if test `chronyc sources -v | tail -1 | cut -d" " -f1` == "^*";then
echo -e "\033[36mok\033[0m"
else
echo -e "\033[31m时钟同步配置失败，请检查干扰项重试\033[0m"
exit
fi
# 在node1上安装ceph-deploy部署工具
echo -e "\033[36m创建ceph集群中\033[0m"
yum install -y ceph-deploy &> /dev/null
mkdir ceph-cluster && cd ceph-cluster

# 创建集群
ceph-deploy new node{1..3} &> /dev/null
test $(ls | wc -w) == 3 && echo "rbd_default_features = 1" >> ceph.conf  # 如果集群节点创建成功 开启快照功能
ceph-deploy mon create-initial &> /dev/null  #初始化monitor
ceph-deploy disk zap node{1..3}:sdb node{1..3}:sdc &> /dev/null # 初始化硬盘
ceph-deploy osd create node{1..3}:sd{b,c} &> /dev/null #创建OSD

# 验证集群
sleep 5
if ceph -s | grep -q -w HEALTH_OK;then
echo -e "\033[36mceph集群创建成功！！\033[0m"
else
echo -e "\033[31mceph集群创建失败，请检查环境！！\033[0m"
fi













